<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 200px;
        height: 30px;
        background-color: pink;
        border: 1px solid #000;
        line-height: 30px;
        font-size: 20px;
        color: #000;
      }
    </style>
  </head>
  <body>
    <button>点击</button>
    <div class="box"></div>
    <script>
      var div = document.querySelector('.box')
      var btn = document.querySelector('button')
      div.innerText = date()
      btn.onclick = function () {
        div.innerText = date()
      }
      function date() {
        var myDate = new Date()
        var y = myDate.getFullYear()
        var m = zeroFill(myDate.getMonth() + 1)
        var d = zeroFill(myDate.getDate())
        var h = zeroFill(myDate.getHours())
        var M = zeroFill(myDate.getMinutes())
        var s = zeroFill(myDate.getSeconds())
        var str = `${y}-${m}-${d} ${h}:${M}:${s}`
        return str
      }
      function zeroFill(num) {
        return num < 10 ? '0' + num : num
      }
    </script>
  </body>
</html>
